package com.bootdo.hotel.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.hotel.domain.HotelEveryDayMoney;
import com.bootdo.hotel.domain.HotelRoomType;
import com.bootdo.hotel.service.HotelEveryDayMoneyService;
import com.bootdo.hotel.service.HotelRoomTypeService;
import com.bootdo.lvyou.domain.LvyouScenicSpot;
import com.bootdo.lvyou.service.LvyouScenicSpotService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.hotel.domain.HotelRoomMoneyCode;
import com.bootdo.hotel.service.HotelRoomMoneyCodeService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 房价码配置
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-18 16:14:42
 */
 
@Controller
@RequestMapping("/hotel/hotelRoomMoneyCode")
public class HotelRoomMoneyCodeController extends BaseController{
	@Autowired
	private LvyouScenicSpotService lvyouScenicSpotService;
	@Autowired
	private HotelEveryDayMoneyService hotelEveryDayMoneyService;
	@Autowired
	private HotelRoomMoneyCodeService hotelRoomMoneyCodeService;
	@Autowired
	private HotelRoomTypeService hotelRoomTypeService;
	@ModelAttribute
	public HotelRoomMoneyCode get(@RequestParam(required=false) Long id) {
		if (id != null){
			return hotelRoomMoneyCodeService.get(id);
		}else{
			return new HotelRoomMoneyCode();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/hotel/hotelRoomMoneyCode','hotel:hotelRoomMoneyCode:hotelRoomMoneyCode',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','hotel:hotelRoomMoneyCode:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("hotel:hotelRoomMoneyCode:hotelRoomMoneyCode")	
	String HotelRoomMoneyCode(HotelRoomMoneyCode hotelRoomMoneyCode,HttpServletRequest request,HttpServletResponse response){
	    return "hotel/hotelRoomMoneyCode/hotelRoomMoneyCode";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("hotel:hotelRoomMoneyCode:hotelRoomMoneyCode")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		if(!isAdmin()){
			query.put("userId", getUserId());
		}
		List<HotelRoomMoneyCode> hotelRoomMoneyCodeList = hotelRoomMoneyCodeService.list(query);
		int total = hotelRoomMoneyCodeService.count(query);
		PageUtils pageUtils = new PageUtils(hotelRoomMoneyCodeList, total);
		return pageUtils;
	}

   ///hotel/hotelRoomMoneyCodegetListByHotelId
	@ResponseBody
	@PostMapping("/getListByHotelId")
	@RequiresPermissions("hotel:hotelRoomMoneyCode:hotelRoomMoneyCode")
	public R getListByHotelId(@RequestParam Map<String, Object> params){
		//查询列表数据
		if(!isAdmin()){
			params.put("userId", getUserId());
		}
		List<HotelRoomMoneyCode> hotelRoomMoneyCodeList = hotelRoomMoneyCodeService.list(params);
	 	return R.ok().put("l",hotelRoomMoneyCodeList);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("hotel:hotelRoomMoneyCode:edit")
	String add(HotelRoomMoneyCode hotelRoomMoneyCode,HttpServletRequest request,HttpServletResponse response){
	    return "hotel/hotelRoomMoneyCode/hotelRoomMoneyCodeedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(HotelRoomMoneyCode hotelRoomMoneyCode) {
		return R.ok().put("i",hotelRoomMoneyCode);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("hotel:hotelRoomMoneyCode:edit")
	public R save( HotelRoomMoneyCode hotelRoomMoneyCode){
		if(!isAdmin()){
			 if(hotelRoomMoneyCode.getUserId() == null){
				 hotelRoomMoneyCode.setUserId(getUserId());
				 hotelRoomMoneyCode.setUserName(getUsername());
			 }
		}
		if(null == hotelRoomMoneyCode.getRoomTypeId()){
			return R.error("请选择一个房型");
		}
		HotelRoomType hotelRoomType =  hotelRoomTypeService.get(hotelRoomMoneyCode.getRoomTypeId());
		hotelRoomMoneyCode.setRoomTypeName(hotelRoomType.getName());

		List<LvyouScenicSpot> lvyouScenicSpots = lvyouScenicSpotService.list(MapBuild.getInstand().setKV("userId", getUserId()));
		if(null != lvyouScenicSpots && lvyouScenicSpots.size() > 0){
			hotelRoomMoneyCode.setHotelId(lvyouScenicSpots.get(0).getId());
			hotelRoomMoneyCode.setHotelName(lvyouScenicSpots.get(0).getName());
		}
		if(hotelRoomMoneyCodeService.save(hotelRoomMoneyCode)>0){
			return R.ok();
		}
		return R.error();
	}


	/**
	 * /hotel/hotelRoomMoneyCode/getEveryDayMoney
	 * 得到每日价格
	 */
	@ResponseBody
	@PostMapping("/getEveryDayMoney")
	@RequiresPermissions("hotel:hotelRoomMoneyCode:edit")
	public R getEveryDayMoney(HotelEveryDayMoney hotelEveryDayMoney
	){
		List<HotelEveryDayMoney> l =  hotelEveryDayMoneyService.list(
				MapBuild.getInstand()
						.setKV("codeId", hotelEveryDayMoney.getCodeId())
		);
		Map map = new HashMap();
		l.forEach(item ->{
			map.put(DateUtils.format(item.getDay()), item.getMoney());
		});
		return R.ok().put("i", map);
	}


	/**
	 * 保存每日价格 /hotel/hotelRoomMoneyCode/saveEveryDayMoney
	 */
	@ResponseBody
	@PostMapping("/saveQuickEveryDayMoney")
	@RequiresPermissions("hotel:hotelRoomMoneyCode:edit")
	public R saveEveryDayMoney(@RequestParam("codeId") Long codeId,
							   @RequestParam("startTime") String startTime,
							   @RequestParam("endTime") String endTime,
							   @RequestParam("money") Double money
	) throws ParseException {

		Date s = DateUtils.pasre(startTime, DateUtils.DATE_PATTERN);
		Date e = DateUtils.pasre(endTime, DateUtils.DATE_PATTERN);
		System.err.println(DateUtils.format(s,DateUtils.DATE_TIME_PATTERN));
		System.err.println(DateUtils.format(DateUtils.getCustomTime(0),DateUtils.DATE_TIME_PATTERN));
		if(s.before(DateUtils.getCustomTime(0))){
			return R.error("开始时间不能小于今天");
		}
		if(s.after(e)){
			return R.error("开始时间不能大于结束时间");
		}
		List<Date> dateList =  DateUtils.getStartTimeAndEndTimeEveryDay(
				s,
				e);
		if(money <= 0){
			return R.error("金额不能为0");
		}
		if(dateList.size() > 32){
			return R.error("时间的间隔不能大于30天");
		}
		dateList.forEach(item->{
			HotelEveryDayMoney hotelEveryDayMoney  =	getFristHotelEveryDayMoney(MapBuild.getInstand().setKV("codeId", codeId).setKV("day",
					DateUtils.format(item))
					);
			if(item.after(DateUtils.getCustomTime(0))){
				if(null == hotelEveryDayMoney){
					hotelEveryDayMoney = new HotelEveryDayMoney();
					hotelEveryDayMoney.setMoney(money);
					hotelEveryDayMoney.setDay(item);
					hotelEveryDayMoney.setCodeId(codeId);
				}else{
					hotelEveryDayMoney.setMoney(money);
				}
				hotelEveryDayMoneyService.save(hotelEveryDayMoney);
			}
		});
		return R.ok();
	}


	/**
	 * 保存每日价格 /hotel/hotelRoomMoneyCode/saveEveryDayMoney
	 */
	@ResponseBody
	@PostMapping("/saveEveryDayMoney")
	@RequiresPermissions("hotel:hotelRoomMoneyCode:edit")
	public R saveEveryDayMoney(@RequestParam("codeId") Long codeId,
							   @RequestParam("day") String day,
							   @RequestParam("money") Double money
				  ) throws ParseException {
		HotelEveryDayMoney temp =getFristHotelEveryDayMoney(MapBuild.getInstand()
				.setKV("codeId", codeId)
				.setKV("day", day));
		 if(null == temp){
		 	 temp = new HotelEveryDayMoney();
		 	 temp.setMoney(money);
		 	 temp.setDay(DateUtils.pasre(day, DateUtils.DATE_PATTERN));
			 temp.setCodeId(codeId);
		 }else{
			 temp.setMoney(money);
		 }
		 hotelEveryDayMoneyService.save(temp);
		return R.ok();
	}


	HotelEveryDayMoney getFristHotelEveryDayMoney(Map map){
		List<HotelEveryDayMoney> l =  hotelEveryDayMoneyService.list(
				map
		);
		if(null == l ||  l.size() == 0){
			 return  null;
		}else{
			return l.get(0);
		}
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("hotel:hotelRoomMoneyCode:edit")
	public R remove( Long id){
		if(hotelRoomMoneyCodeService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("hotel:hotelRoomMoneyCode:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		hotelRoomMoneyCodeService.batchRemove(ids);
		return R.ok();
	}
	
}